<?php
if (!defined('APP_ID')) { die('No direct script access allowed'); }

abstract class Database_Query_Mysql {
	public function value($value, $type = null) {
		$result = "";
		if (empty($type)) {
			if (is_int($value)) {
				$type = Query::DATA_INTEGER;
			} else {
				$type = Query::DATA_STRING;
			}
		}
		
		if (is_null($value)) {
			$result = "NULL";
		} else {
			switch ($type) {
				case Query::DATA_FUNCTION:
					$result = Database::escape($value);
					break;
				case Query::DATA_SERIALIZE:
					$result = Database::escape(serialize($value));
					break;
				case Query::DATA_INTEGER:
					$result = intval($value);
					break;
				case Query::DATA_STRING:
				default: 
					$result = "'" . Database::escape($value) . "'";
				
			}
		}
		
		return $result;
	}
	
	public function execute() {
		return Database::execute($this->compile());
	}
	
	public function parse($arr) {
		$result = '';

		$temp = array();
		foreach ($arr as $item) {
			if (count($item) > 1) {
				$temp[] = call_user_func_array('sprintf', $item);
			} else if (!empty($item)) {
				$temp[] = $item[0];
			}
		}

		if (!empty($temp)) {
			$result = implode(' AND ', $temp);
		}

		return $result;
	}
}
